.NET 9.0.100 到 .NET 10.0.100 之后 Razor 的重大更改

本文档列出了 .NET 9 常规版本(.NET SDK 版本 9.0.100)到 .NET 10 常规版本(.NET SDK 版本 10.0.100)之后 Razor 中已知的中断性变更。

预处理器指令分析中断

VS 17.13p1 和 .NET 9.0.200 中引入

引入了新的词法模式,用于了解 razor 文件中的 C# 节,从而增加了与 C# 本机词法的兼容性。 但是,这也给 Razor 编译器对 C# 预处理指令的理解带来了一些中断性变更,而 C# 预处理指令以前无法一致地工作。 指令现在需要从 Razor 文件中的行开头开始(只允许在它们之前使用空格)。 此外,当预处理器块被视为非活动时 #if ,Razor 编译器现在已正确禁用已禁用的节。

需要预处理器块才能从行的开头开始

razor
@{ #if DEBUG /* Previously allowed, now triggers RZ1043 */ }
<div>test</div>
@{ #endif /* Previously allowed, now triggers RZ1043 */ }

若要修复,请将指令移动到新行。 指令之前只允许空格。

razor
@{
#if DEBUG /* This is allowed */
}
<div>test</div>
@{
    #endif /* This is allowed */
}

禁用的块现在在 Razor 编译器中被视为正确

禁用的块现在被 Razor 编译器完全禁用,并且不会尝试了解块。 与上一个中断结合使用时,这意味着如果#else#elif#endif行(模态空格)的开头或未处于行(modulo 空格),则文件的较大部分将被视为禁用,而不是在旧版本的 Razor 编译器中。 为了帮助诊断此处的潜在中断,Razor 编译器将扫描已禁用的文本部分,了解潜在的错放预处理器指令,并在遇到错误时报告警告。

razor
@{
#if false
}

This area is now properly considered disabled by the razor compiler, and no attempt to understand it as either C# or HTML is made. This
can cause changes to how the output is rendered from previous versions of the Razor compiler.

@{ #else
    In previous versions of the Razor compiler, this directive would have been picked up. It is no longer picked up because it is not at
    the start of a line. The Razor compiler will report a warning, RZ1044, to help diagnose any potential breaks in this area.
}

@{
#endif
}